package com.AEWMS.Service;

import com.AEWMS.Dao.Admin;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class AdminService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public AdminService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("StudentService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有管理员
     * @return 所有管理员信息
     */
    public ArrayList<Admin> findAllAdmin(){
        ArrayList<Admin> allAdminList = new ArrayList<>();//升级版数组，返回所有管理员对象
        String sql = "SELECT * FROM admin_info";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                Admin s = new Admin();//按行查找，为false时查询结束
                s.setAdminId(rs.getString("admin_id"));
                s.setAdminName(rs.getString("admin_name"));
                s.setAdminSex(rs.getString("admin_sex"));
                s.setType(rs.getString("type"));
                s.setAdminTel(rs.getString("admin_tel"));
                s.setAdminPassword(rs.getString("admin_password"));
                s.setStuNum(rs.getString("student_number"));
                allAdminList.add(s);//将查找到的行（每个管理员对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllAdmin 失败");
            System.out.println(e);
        }
        return allAdminList;//输出包含所有学生对象的数组
    }

    /**
     * 查找管理员
     * @param id 通过id
     * @return 返回一个管理员对象
     */
    public Admin findAdmById(String id){
        Admin s = new Admin();
        String sql = "SELECT * FROM admin_info WHERE admin_id = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setAdminId(rs.getString("admin_id"));
                s.setAdminName(rs.getString("admin_name"));
                s.setAdminSex(rs.getString("admin_sex"));
                s.setType(rs.getString("type"));
                s.setAdminTel(rs.getString("admin_tel"));
                s.setAdminPassword(rs.getString("admin_password"));
                s.setStuNum(rs.getString("student_number"));
            }
        }
        catch (Exception e) {
            System.out.println("findAdmById 失败");
            System.out.println(e);
        }
        return s;
    }
    /**
     * 查找管理员
     * @param stuNum 通过id
     * @return 返回一个管理员对象
     */
    public Admin findAdmByIdNum( String stuNum){
        Admin s = new Admin();
        String sql = "SELECT * FROM admin_info WHERE student_number = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,stuNum);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setAdminId(rs.getString("admin_id"));
                s.setAdminName(rs.getString("admin_name"));
                s.setAdminSex(rs.getString("admin_sex"));
                s.setType(rs.getString("type"));
                s.setAdminTel(rs.getString("admin_tel"));
                s.setAdminPassword(rs.getString("admin_password"));
                s.setAdminPassword(rs.getString("student_number"));
            }
        }
        catch (Exception e) {
            System.out.println("findAdmById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 添加管理员
     * @return 一个管理员对象
     */
    public boolean insertAdm(Admin s){
        boolean flag = false;
        String sql = "INSERT INTO admin_info VALUES(?,?,?,?,?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);//sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setString(1,s.getAdminId());
            this.pstmt.setString(2,s.getAdminPassword());
            this.pstmt.setString(3,s.getAdminName());
            this.pstmt.setString(4,s.getAdminSex());
            this.pstmt.setString(5,s.getAdminTel());
            this.pstmt.setString(6,s.getType());
            this.pstmt.setString(7,s.getStuNum());

            int rs = this.pstmt.executeUpdate();//这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertAdm defeat");
            System.out.println(e);
        }
        return flag;
    }
    /**
     * 添加管理员
     * @return 一个管理员对象
     */
    public boolean insertClassAdm(Admin s){
        boolean flag = false;
        String sql = "INSERT INTO admin_info VALUES(?,123456,?,?,?,'class_admin',?)";
        try {
            this.pstmt = conn.prepareStatement(sql);
            //sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setString(1,s.getAdminId());
            this.pstmt.setString(2,s.getAdminName());
            this.pstmt.setString(3,s.getAdminSex());
            this.pstmt.setString(4,s.getAdminTel());
            this.pstmt.setString(5,s.getStuNum());
            int rs = this.pstmt.executeUpdate();
            //这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertClassAdm defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个管理员
     * @param  id,password
     * @return 是否成功
     */
    public boolean updateAdm(String id,String password) {
        boolean flag = false;
        String sql = "UPDATE admin_info SET  admin_password = ? WHERE admin_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,password);
            this.pstmt.setString(2,id);//准备对象里用的是参数索引值，参数和问号的顺序对应，因为是将传进来的内容放在准备对象里
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updateAdm 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个管理员
     * @param id 管理员id
     * @return 是否成功
     */
    public boolean deleteAdmById(String id){
        boolean flag = false;
        String sql = "DELETE FROM admin_info WHERE admin_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deleteAdmById 失败");
            System.out.println(e);
        }
        return flag;
    }

}

